package com.icss.train_sys_back.mapper.ticket_manage;

import com.icss.train_sys_back.entity.home.MyTicketEntity;
import com.icss.train_sys_back.entity.ticket_manage.TicketEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/10/2 14:16
 */
@Mapper
public interface TicketManageMapper {
    //车票分页+条件查询
    List<TicketEntity> getListConditions(String trainNumberId, String startStationId, Integer endStationId, Integer status);

    //更新车票信息
    @Update(" update t_ticket set train_number_id = #{trainNumberId},carriage_type = #{carriageType} , " +
            " start_station_id = #{startStationId},arrival_station_id = #{arrivalStationId}," +
            " departure_time = #{departureTime} , arrival_time = #{arrivalTime},ticket_price = #{ticketPrice}," +
            " total_tickets = #{totalTickets},remaining_tickets = #{remainingTickets},ticket_status = #{ticketStatus}" +
            " where ticket_id = #{ticketId}")
    void update(TicketEntity ticket);

    //更新车票状态
    @Update("update t_ticket set  ticket_status = #{ticketStatus} where ticket_id = #{ticketId} ")
    boolean changeStatus(String ticketId, Integer ticketStatus);

    //删除车票
    @Delete("delete from t_ticket where ticket_id = #{ticketId};")
    boolean delete(String ticketId);

    //添加车票
    @Insert("insert into t_ticket values (#{ticketId},#{trainNumberId},#{carriageType}," +
            "#{startStationId},#{arrivalStationId},#{departureTime},#{arrivalTime}," +
            "#{ticketPrice},#{totalTickets},#{remainingTickets},#{ticketStatus})")
    boolean add(TicketEntity ticket);

    // // 根据城市获取列车票务
    @Select({"<script>",
            "SELECT ticket_id, train_number_id, carriage_type, start_station_id, arrival_station_id, ",
            "       departure_time, arrival_time, ticket_price, total_tickets, remaining_tickets, ticket_status ",
            "FROM t_ticket ",
            "WHERE ticket_status = 1 ", // 只查询开售状态的车票
            "AND start_station_id in (select station_id from t_station where city=#{startCity})",
            "AND arrival_station_id in (select station_id from t_station where city=#{endCity})",
            "<if test='goDate != null'>",
            "AND DATE(departure_time) = #{goDate}",
            "</if>",
            "</script>"})
    List<TicketEntity> getTrainTickets(String startCity, String endCity, String goDate);

    //查车票表里的车次train_number_id
    @Select("select train_number_id from t_ticket where ticket_id = #{ticketId}")
    Integer getTicketsByTicketId(String ticketId);

    //查询本人车票
    @Select("select * from t_order o left join t_ticket t on o.ticket_id = t.ticket_id \n" +
            "left join t_order_passenger op on o.order_id = op.order_id \n" +
            "LEFT JOIN t_seat se on op.seat_id = se.seat_id\n" +
            "LEFT JOIN t_passenger p on op.passenger_id = p.passenger_id \n" +
            "LEFT JOIN t_passenger_user pu on p.passenger_id = pu.t_passenger_id\n" +
            "where pu.t_user_id = #{userId}")
    List<MyTicketEntity> listByUserId(Integer userId);

    //车票数量减少
    @Update("update t_ticket set remaining_tickets = remaining_tickets - #{size} where ticket_id = #{ticketId}")
    void updateTicketNumber(String ticketId, int size);

    //车票数量增加
    @Update("update t_ticket set remaining_tickets = remaining_tickets + #{size} where ticket_id = #{ticketId}")
    void incrTicketNumber(String ticketId, int size);

    // 批量插入
    void batchAdd(List<TicketEntity> ticketEntityList);
}
